package com.cy.ppj.service.account.impl;

import com.cy.ppj.dao.mapper.account.AccountMapper;
import com.cy.ppj.dao.mapper.account.DealRecordMapper;
import com.cy.ppj.dao.mapper.account.CommissionRecordMapper;
import com.cy.ppj.model.domian.account.DealRecord;
import com.cy.ppj.model.dto.account.AccountAmountDTO;
import com.cy.ppj.service.BaseService;
import com.cy.ppj.service.account.AccountService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * @description: 账户
 * @author: Ocean
 * @create: 2018-07-24 10:31
 **/
@Service
public class AccountServiceImpl extends BaseService implements AccountService {

    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private DealRecordMapper dealRecordMapper;

    @Override
    @Transactional
    public void transaction(AccountAmountDTO accountAmountDTO) {
        Date now = accountAmountDTO.getUpdateTime();
        //更新余额
        int result = accountMapper.updateAmount(accountAmountDTO);
        state(result == 1, "用户个人账户余额更新失败");
        //保存交易记录
        DealRecord dealRecord = new DealRecord();
        dealRecord.setUserId(accountAmountDTO.getUserId());
        dealRecord.setRelationId(accountAmountDTO.getRelationId() == null ? 0L : accountAmountDTO.getRelationId());
        dealRecord.setAmount(accountAmountDTO.getAmount());
        dealRecord.setType(accountAmountDTO.getType());
        dealRecord.setCreateTime(now);

        String remark = StringUtils.isEmpty(accountAmountDTO.getRemark()) ? accountAmountDTO.getType().getText() : accountAmountDTO.getRemark();

        dealRecord.setRemark(remark);
        //保存资金流水记录
        dealRecordMapper.insertOne(dealRecord);

    }
}
